//
//  HYStatus.swift
//  iOSWeibo
//
//  Created by why on 16/7/25.
//  Copyright © 2016年 王浩宇. All rights reserved.
//

import UIKit

class HYStatus: NSObject {
    
    ///微博ID
    var id: Int64 = 0
    
    ///微博创建时间
    var created_at: String?
    
    ///微博信息内容
    var text: String?
    
    ///微博来源
    var source: String?
    
    /// 微博配图    // [Int] , [String: AnyObjcet] -> [[String: AnyObjcet]]
    /*
    [
    {thumbnail_pic:http://ww4.sinaimg.cn/thumbnail/d00970e7jw1f64ogqi4l2j20c80c7gm5.jpg},
    {thumbnail_pic:http://ww4.sinaimg.cn/thumbnail/d00970e7jw1f64ogqi4l2j20c80c7gm5.jpg}
    ]
    
    */
    var pic_urls: [[String: AnyObject]]? {
        didSet {
            if pic_urls?.count == 0 {
                return
            }
            
            //数组中有字典
            //创建数组保存URL
            storePicutureURLs = [NSURL]()
            
            for dict in pic_urls! {
                let urlString = dict["thumbnail_pic"] as? String
                
                let url = NSURL(string: urlString!)
                storePicutureURLs?.append(url!)
            }
        }
    }
    
    ///直接存放图片的URL
    var storePicutureURLs: [NSURL]?
    
    ///转发数
    var reposts_count: Int = 0
    
    ///评论数
    var comments_count: Int = 0
    
    ///表态数
    var attitudes_count: Int = 0
    
    ///用户模型
    var user: HYUser? {
        didSet {
            // 设置的是字典
            // 在字典转模型
            // 重新给自己赋值
            // 在didSet里面再次给自己赋值不会调用didSet
            //            user = HMUser(dict: [])
        }
    }
    
    /// 被转发微博模型,不一定有
    var retweeted_status: HYStatus?
    
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user" {
            // 自己来字典转模型不要系统帮我们设置字典
            // 将value转成字典
            if let dict = value as? [String: AnyObject] {
                // 字典转模型
                user = HYUser(dict: dict)
            }
            
            return
        }
        
        /// 被转发微博
        if key == "retweeted_status" {
            // 自己来字典转模型
            if let dict = value as? [String: AnyObject] {
                // 字典转模型
                retweeted_status = HYStatus(dict: dict)
            }
            
            return
        }
        
        // 其他key走正常的 setValue:forKey:
        super.setValue(value, forKey: key)
        
    }
    
    ///字典中的key在模型中找不到对应的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    /// 对象打印
    override var description: String {
        get {
            let keys = ["id", "created_at", "text", "source", "pic_urls", "reposts_count", "comments_count", "attitudes_count"]
            // 根据传入的keys,分别拿每一个key去模型找找对应的value,找到了值之后,用key和value拼接成字典
            return "\n\n 微博模型: " + dictionaryWithValuesForKeys(keys).description
        }
    }
}
